package com.ysu.his.dispensary.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysu.his.dispensary.mapper.DrugDetailMapper;
import com.ysu.his.dto.DrugDto;
import com.ysu.his.entity.drug.Drug;
import com.ysu.his.dispensary.mapper.DrugMapper;
import com.ysu.his.dispensary.service.DrugService;
import com.ysu.his.entity.drug.DrugDetail;
import com.ysu.his.exception.HISException;
import com.ysu.his.vo.DrugVo;
import com.ysu.his.vo.common.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 药品相关业务实现类
 */
@Service
public class DrugServiceImpl implements DrugService
{
    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private DrugDetailMapper drugDetailMapper;

    @Override
    public PageInfo<DrugVo> getDrugByPage(Integer page, Integer limit, DrugDto drugDto)
    {
        PageHelper.startPage(page, limit);
        List<DrugVo> list = drugMapper.getDrugByPage(drugDto);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public void addDrug(Drug drug)
    {
        DrugDetail drugDetail = new DrugDetail();

        BeanUtils.copyProperties(drug, drugDetail);
        int result = drugMapper.insertDrug(drug);
        drugDetail.setDrugId(drug.getId());
        int resultDetail = drugDetailMapper.insertDrugDetails(drugDetail);

        if (result > 0 && resultDetail > 0)
        {
            return;
        }

        throw new HISException(ResultCodeEnum.DRUG_OPERATION_FAILED);
    }

    @Override
    public Drug getDrugById(Long id)
    {
        Drug drug = drugMapper.getDrugById(id);
        if (drug == null)
        {
            throw new HISException(ResultCodeEnum.DRUG_NOT_EXIST);
        }
        return drug;
    }

    @Override
    @Transactional
    public void updateDrug(Drug drug)
    {

        // 先检查药品是否存在
        Drug existingDrug = drugMapper.getDrugById(drug.getId());
        if (existingDrug == null)
        {
            throw new HISException(ResultCodeEnum.DRUG_NOT_EXIST);
        }

        DrugDetail drugDetail = new DrugDetail();
        int result = 0;
        try
        {
            result = drugMapper.updateDrug(drug);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(drug, drugDetail);

        drugDetail.setDrugId(drug.getId());
        int resultDetail = 0;
        try
        {
            resultDetail = drugDetailMapper.updateDrugDetail(drugDetail);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        if (result > 0 && resultDetail > 0)
        {
            return;
        }
        throw new HISException(ResultCodeEnum.DRUG_OPERATION_FAILED);

    }

    @Override
    public void deleteDrugById(Long id)
    {
        try
        {
            Drug drug = new Drug();
            drug.setId(id);
            drug.setUpdateTime(new Date());

            int result = drugMapper.deleteDrugById(drug);

        } catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException("删除失败: " + e.getMessage());
        }
    }
} 